// pages/regist/regist.js
const util = require('../../utils/util.js')
const net = require('../../utils/request.js');
const app = getApp();

var key = require('../../utils/MD5.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeImage: app.globalData.service +"welfare/index/verify?openid="+app.globalData.openid,
    openid:'openid',
    checkCode:'',//图形验证码,
    phonenumber:'',
    checkMesCode:'',
    ifselect:1,
    nickName: '',
    avatarUrl: '',
    btnGetMescode: '获取验证码',
    btnGetMescodeIs:1,
    channel:'1',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var openid = wx.getStorageSync("openid");
    var channels = wx.getStorageSync('channel');
    if(channels){
      that.setData({
        channel:channels,
      });
    }

    if (openid && openid.length != 0) {
      that.refreshImg();
    } else {
      that.getopenid();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    console.log("" + wx.getStorageSync("openid"));
    var openid = wx.getStorageSync("openid");
    if(openid && openid.length != 0){
      that.refreshImg();
    }else{
      that.getopenid();
    }
  },
  refrshImage:function(){
    var that = this;
    var timestamp = Date.parse(new Date());
    var openid = wx.getStorageSync("openid");

    that.setData({
      codeImage: app.globalData.service + 'welfare/index/verify?openid=' + openid + '&time=' + timestamp,
    })
  },
  getopenid: function () {
    var that = this;
    wx.login({
      success(res) {
        if (res.code) {
          var code = res.code;
          net.requestHttp('welfare/index/getopenid',
            { code: code }, function (res) {
              console.log(res);
              if (res.data.state == 1) {
                wx.setStorage({
                  key: "openid",
                  data: res.data.data.openid
                }),
                that.setData({
                  openid: res.data.data.openid,
                });
                wx.getUserInfo({
                    success: function (ress) {
                      that.setData({
                        nickName: ress.userInfo.nickName,
                        avatarUrl: ress.userInfo.avatarUrl,
                      });
                    }
                  });
                app.globalData.openid = res.data.data.openid;
                app.globalData.session_key = res.data.data.session_key;
                that.refrshImage();
              } else {
                //登录失败
              }
            }
          )
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },

  // 短信验证码
  getmesMessage:function(){
    var that = this;
    var openid = wx.getStorageSync("openid");

    //手机号
    console.log('这是请求短信的openid' + openid);
    if (that.data.checkCode.length<4){
      wx.showToast({
        title: '请输入正确的验证码',
        icon:'none',
      })
      return;
    }
    if(that.data.phonenumber.length<11){
      wx.showToast({
        title: '请输入正确的手机号',
        icon:'none',
      })
      return;
    }

    wx.getUserInfo({
      success: function (ress) {
        if (openid) {
          net.requestHttp('welfare/index/getmobile',
            { mobile: that.data.phonenumber, imgcode: that.data.checkCode, openid: openid }, function (res) {

              if (res.data.state == 1) {
                wx.showToast({
                  title: '发送验证码成功',
                })

                var currentTime = 60;
                var interval = setInterval(function () {
                  that.setData({
                    btnGetMescode: '倒计时' + (currentTime - 1) + '秒',
                    btnGetMescodeIs: 0
                  });
                  currentTime--;
                  if (currentTime <= 0) {
                    clearInterval(interval)
                    that.refreshImg();
                    that.setData({
                      btnGetMescode: '重新获取',
                      btnGetMescodeIs: 1,
                    })
                  }
                }, 1000);
              } else {

              }
            }
          )
        } else {
          //请求一下 openid ,再掉用
          that.getopenid();
        }
      }
    });
    
  },
  checkboxChange:function(e){
    var that = this;

    console.log(e.detail.value);

    if(e.detail.value.length>0){
      console.log('选中')
      that.setData({
        ifselect:1,
      });
    }else{
      console.log('未选中')
      that.setData({
        ifselect: 0,
      });
    }
  }, 
  registAction:function(e){
    var that = this;
    var openid = wx.getStorageSync("openid");
    wx.getUserInfo({
      success: function (res) {
        that.setData({
          nickName: res.userInfo.nickName,
          avatarUrl: res.userInfo.avatarUrl,
        });
      },
      fail: function (res) {
      },
    });
    wx.getUserInfo({
      success: function (ress) {
        if (that.data.ifselect == 1) {
          net.requestHttp('welfare/index/register',
            { mobile: that.data.phonenumber, smscode: that.data.checkMesCode, openid: openid, headimgurl: that.data.avatarUrl, nickname: that.data.nickName, form_id: e.detail.formId ,channel:that.data.channel}, function (res) {

              if (res.data.state == 1) {
                wx.showToast({
                  title: '注册成功',
                })
                wx.setStorage({
                  key: 'userInfo',
                  data: res.data.data,
                })
                app.globalData.userInfo = res.data.data;
                wx.navigateBack({});
              } else {
                that.refrshImage();
              }
            }
          )
        } else {
          wx.showToast({
            title: '请选择是否阅读服务协议',
            icon: 'none',
          })
        }
      }
    });
    
    
  }
  ,
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //输入图片验证码
  inputImageCode: function (e) {
    var that = this
    that.setData({
      checkCode: e.detail.value
    })
  },
  inputphoneNumber:function(e){
    var that = this
    that.setData({
      phonenumber: e.detail.value
    })
  },
  inputPhoneCode:function(){
    var that = this
    that.setData({
      checkMesCode: e.detail.value
    })
  },
  inputcheckMesCode:function(e){
    var that = this
    that.setData({
      checkMesCode: e.detail.value
    })
  },
  refreshImg:function(e){
    var that = this;
    that.refrshImage();
  },
  loginpage:function(e){
    wx.redirectTo({
      url: '../logs/logs',
    })
  },
  webViewPageOne:function(e){
   
    wx.navigateTo({
      url: '../webViewPage/webviewpage?state=1',
    })
  },
  webViewPageTwo: function (e) {
  
    wx.navigateTo({
      url: '../webViewPage/webviewpage?state=2',
    })
  },
})